import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
import 'rxjs/add/operator/toPromise';

import { ClassEntity, Search } from '../components/Interfaces/ClassEntity';

const httpOptions = {
  headers: new Headers({
    'Content-Type': 'application/json',
  })
};

@Injectable()
export class ClassService {

    private apiUrl = '/api/v1/class';  // URL to web api
    constructor(
        private http: Http,
    ) { }

    getClassEntities(): Observable<any> {
        return this.http.get(this.apiUrl, httpOptions).map(res => res.json());
    }

    addClassEntity(classEntity: ClassEntity): Observable<any> {
        return this.http.post(this.apiUrl, classEntity, httpOptions).map(res => res.json());
    }

    editClassEntity(classEntity: ClassEntity): Observable<any> {
        return this.http.put(this.apiUrl + '/' + classEntity.id, classEntity, httpOptions).map(res => res.json());
    }

    deleteClassEntity(classEntity: ClassEntity): Observable<any> {
        return this.http.delete(this.apiUrl + '/' + classEntity.id, httpOptions)
                .map(res => res.json());
    }

    // 条件查询
    getClassEntitiesByCondition(condition: Search): Observable<any> {
        // this.resetApiUrl();
        let apiUrl = this.apiUrl;
        // if (condition.class_name && condition.term_id) {
        //     apiUrl = apiUrl + '?';
        // }
        if ( apiUrl.indexOf('?') !== -1 && condition.class_name) {
            apiUrl = apiUrl + '&class_name=' + condition.class_name;
        }
        if ( apiUrl.indexOf('?') === -1 && condition.class_name) {
            apiUrl = apiUrl + '?class_name=' + condition.class_name;
        }


        if ( apiUrl.indexOf('?') !== -1 && condition.term_id) {
            apiUrl = apiUrl + '&term_id=' + condition.term_id;
        }
        if ( apiUrl.indexOf('?') === -1 && condition.term_id) {
            apiUrl = apiUrl + '?term_id=' + condition.term_id;
        }
        if ( apiUrl.indexOf('?') !== -1 && condition.page) {
            apiUrl = apiUrl + '&page=' + condition.page;
        }
        if ( apiUrl.indexOf('?') === -1 && condition.page) {
            apiUrl = apiUrl + '?page=' + condition.page;
        }
        if ( apiUrl.indexOf('?') !== -1 && condition.all) {
            apiUrl = apiUrl + '&all=1';
        }
        if ( apiUrl.indexOf('?') === -1 && condition.all) {
            apiUrl = apiUrl + '?all=1';
        }

        if ( apiUrl.indexOf('?') !== -1 && condition.start_time) {
            apiUrl = apiUrl + '&start_time=' + condition.start_time;
        }
        if ( apiUrl.indexOf('?') === -1 && condition.start_time) {
            apiUrl = apiUrl + '?start_time=' + condition.start_time;
        }
        if ( apiUrl.indexOf('?') !== -1 && condition.end_time) {
            apiUrl = apiUrl + '&end_time=' + condition.end_time;
        }
        if ( apiUrl.indexOf('?') === -1 && condition.end_time) {
            apiUrl = apiUrl + '?end_time=' + condition.end_time;
        }

        if ( apiUrl.indexOf('?') !== -1 && condition.search_type) {
            apiUrl = apiUrl + '&search_type=' + condition.search_type;
        }
        if ( apiUrl.indexOf('?') === -1 && condition.search_type) {
            apiUrl = apiUrl + '?search_type=' + condition.search_type;
        }

        return this.http.get(apiUrl, httpOptions).map(res => res.json());
        // throw new Error("Method not implemented.");
    }

    resetApiUrl() {
        this.apiUrl = '/api/v1/class';
    }
}
